package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.util.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
@Slf4j
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    /**
     * 1. 列表
     */
    @RequestMapping("/list")
    public String list(String contractId,String contractProductId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行货物的附件列表查询开始...");
        // A. 根据货物id查询附件
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo =
                extCproductService.findByPage(example, pageNum, pageSize);

        // B. 查询附件的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        // C. 保存
        request.setAttribute("pageInfo", pageInfo);
        request.setAttribute("factoryList", factoryList);
        // 附件表 co_ext_cproduct （id、contract_product_id、contract_id ）
        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);
        return "cargo/extc/extc-list";
    }

    /**
     * 2. 附件添加、修改
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct)  {
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(extCproduct.getId())) {
            extCproductService.save(extCproduct);
        } else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list?contractId="+extCproduct.getContractId()
                +"&contractProductId="+extCproduct.getContractProductId();
    }

    /**
     * 3.extc-list.jsp点击修改，进入到extc-update.jsp页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId) {
        //1. 根据附件id查询
        ExtCproduct extCproduct = extCproductService.findById(id);
        //2. 查询附件厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        //3. 保存
        request.setAttribute("extCproduct", extCproduct);
        request.setAttribute("factoryList", factoryList);
        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);
        return "cargo/extc/extc-update";
    }

    /**
     * 4. 删除附件
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list?contractId="+contractId
                +"&contractProductId="+contractProductId;
    }

}